/**
 * 
 */
package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.OrderSumMain;
import com.tc.dispatch.gsservice.model.PeiFangPrice;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.FloatJsonValueProcessor;
import com.tc.dispatch.web.util.WidgtFactory;
import com.tc.dispatch.web.util.tools;
import com.tc.dispatch.web.util.check.CheckValid;

/**
 * @author cajeep
 * 
 */
public class peiFang extends HttpServlet {
	private static final String CONTENT_TYPE = "text/html; charset=utf-8";


	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public Float getPeiFang(int fac,String ssign, int month1, int year1) {
		GSServiceLocal gss = gsHelper.getGSServiceLocal();

		Float ret = new Float(0);
		try {
			ret = gss.getPeiFangPrice(fac, ssign, month1, year1).getPrice(month1);
		} catch (GSException gse) {
			System.out.println("getPeiFang error :" + gse.getMessage());
			return ret;
		}
		return ret;

	}
	public boolean storePeiFang(String ssign,String factory,Float price,Date date1)
	{
		GSServiceLocal gss = gsHelper.getGSServiceLocal();

		Float ret = new Float(0);
		try {
			return gss.storePeiFangPrice(ssign, factory, price, date1);
		} catch (GSException gse) {
			System.out.println("storePeiFang error :" + gse.getMessage());
			return false;
		}
	}
	public boolean removeRecord(int id) {
		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		try {
			gss.removePeiFangPrice(id);
		} catch (GSException gse) {
			System.out.println("peifang.removeRecord error :" + gse.getMessage());
			return false;
		}
		return true;

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();

		CheckValid rcv = WidgtFactory.getRequestCheckValid(request);
		CheckValid scv = WidgtFactory.getSimpleCheckValid();
		String action = (String) rcv.checkString("action");
		System.out.println("enter into peifang dopost");
		if (action.equals("list")) {
			Integer start = (Integer) rcv.checkInteger("start");

			Integer limit = (Integer) rcv.checkInteger("limit", 10);
			
			
			try {
				List ret=gss.findPeiFangPriceByPages("", start.intValue(), limit.intValue());
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.lang.Float.class,
						new FloatJsonValueProcessor());

				// BeanUtils.copyProperties();

				json.put("totalProperty", new Integer(gss
						.getPeiFangPriceTotalNums("")));
				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);
				response.getWriter().print(json);
			} catch (GSException e) {
				System.out.println("findPeiFangPriceByPages error:" + e.getMessage());
				out.print("{success:false, data:'false'}");
			}
		} else if (action.equals("update")) {
			Integer id=(Integer)rcv.checkInteger("id");
			try {
				String ssign=(String)rcv.checkString("ssign");
				String factory=(String)rcv.checkString("factory");
				Float month1=(Float)rcv.checkFloat("month_1");
				Float month2=(Float)rcv.checkFloat("month_2");
				Float month3=(Float)rcv.checkFloat("month_3");
				Float month4=(Float)rcv.checkFloat("month_4");
				Float month5=(Float)rcv.checkFloat("month_5");
				Float month6=(Float)rcv.checkFloat("month_6");
				Float month7=(Float)rcv.checkFloat("month_7");
				Float month8=(Float)rcv.checkFloat("month_8");
				Float month9=(Float)rcv.checkFloat("month_9");
				Float month10=(Float)rcv.checkFloat("month_10");
				Float month11=(Float)rcv.checkFloat("month_11");
				Float month12=(Float)rcv.checkFloat("month_12");
				String bz=(String)rcv.checkString("bz");
				java.util.Date date1 = new Date();
				int year1=Calendar.getInstance().getTime().getYear()+1900;
				
				PeiFangPrice pf=new PeiFangPrice(id.intValue(),ssign,factory,year1,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12,bz,date1);
				if(id.intValue()==0)
				{
					gss.createPeiFangPrice(pf);
				}
				else
					gss.updatePeiFangPrice(pf);
				
			} catch (NumberFormatException nfe) {
				System.out.print("numberfomat error!:" + nfe.getMessage());
			} catch (GSException gse) {
				System.out.println("storePeiFangPrice error:" + gse.getMessage());
				out.print("{success:false, data:'false'}");
			}
			response.getWriter().print("{success:true}");

		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
